/**
 * Created by gaoyj on 2018/6/28.
 */
window.onload = function (ev) {
    topbar.hide();
};

var faceForm = $("#faceForm").validate({
    rules: {
        operator: {
            required: true,
        },
        discount: {
            required: true
        }
    }
});
var str;
$.post("productPrice/findModelPrice", null, function (data) {
    var group_obj = data;
    for (var i = 0; i < group_obj.length; i++) {
        str += '<option value="' + group_obj[i].id + '">' + group_obj[i].modelName + '</option>'
    }
    $("#supplyModel").append(str);
});
var adminRole = roleCheck();
$('#dataTable').bootstrapTable({
    url: '/productPrice/findWorkNumberPricePage',
    height: $(window).height() - 200,
    pageSize: 30,
    pagination: true,
    pageList: [30, 50, 100],
    clickToSelect: true,
    queryParams: queryParams,
    uniqueId: 'id',
    queryParamsType: '',
    sidePagination: 'server',
    responseHandler: function (res) {
        return {
            "total": res.total,
            "rows": res.resultList
        };
    },
    onClickRow: function (row) {
        if (row.account != null && row.account != '') {
            $("#account_field" + row.id + row.account).html(row.account);
        }
    },
    columns: [
        {
            width: '3px',
            align: 'center',
            formatter: function (v, row, index) {
                return index + 1
            }
        }, {
            width: '20px',
            align: 'center',
            field: 'account',
            title: '会员编号',
            formatter: function (v, o) {
                return copyAddon(v) + (adminRole == 1 ? v : userAccountHidden(o.account, o.id + o.account));
            }
        }, {
            width: '20px',
            align: 'center',
            field: 'name',
            title: '会员名称'
        }, {
            width: '20px',
            align: 'center',
            field: 'productType',
            title: '运营商',
            formatter: function (v, row, index) {
                return operatorFun[v];
            }
        }, {
            width: '40px',
            align: 'center',
            field: 'minPrice',
            title: '最小折扣'
        }, {
            width: '40px',
            align: 'center',
            field: 'maxPrice',
            title: '最大折扣'
        }, {
            width: '40px',
            align: 'center',
            field: 'adminName',
            title: '操作人'
        }, {
            width: '40px',
            align: 'center',
            field: 'modifyTime',
            title: '操作时间',
            formatter: function (v, row, index) {
                return format(v);
            }
        }
    ]
});

$("#search").click(function () {
    $("#dataTable").bootstrapTable("refresh");
});
$("#updateBtn").click(function () {
    if ($('input[name=faceValue]:checked').length < 1) {
        layer.msg("请选择面值");
        return;
    }
    var v = $("#faceForm").valid();
    if (!v) {
        return;
    }
    $.ajax({
        type: "POST",
        url: "/price/updateDiscount",
        traditional: true,
        data: $("#faceForm").serialize(),
        dataType: "json",
        success: function (data) {

            if (data.errorCode == 200) {
                $("#dataTable").bootstrapTable("refresh");
                layer.msg(data.errorMsg);
                $("#myModal").modal("hide");
            } else {
                layer.msg(data.errorMsg)
            }
        },
        beforeSend: function () {
        }
    });
})

function updateFace(v) {
    changeOperator("MOBILE");
    $("#discount").val("");
    faceForm.resetForm();
    $("#JSESSIONID").val(sessionStorage.getItem('JSESSIONID'))
    $("#myModal").modal("show");
}

function changeOperator(v) {
    v = $("#operator").val();
    if (v == "SINOPEC" || v == "CNPC") {
        // $("#cardType").attr("name", "cardType");
        $("#cardTypeDiv").fadeIn();
    } else {
        // $("#cardType").attr("name", "cardType");
        $("#cardTypeDiv").fadeOut();
        $("#cardType").val("0");

    }
    $("#faceList").empty();
    $.ajax({
        url: "/price/queryFaceValue",
        data: {operator: v},
        dataType: 'JSON',
        async: false,
        type: 'GET',
        success: function (data) {
            if (data.errorCode == 200) {
                data.data.forEach(function (v) {
                    $("#faceList").append('<label class="icheck" style="margin-right:10px;"> <input name="faceValue" type="checkbox" value="' + v + '"> ' + v + ' </label> ')
                })
                $('.icheck').iCheck({
                    checkboxClass: 'icheckbox_square-blue checkbox',
                    radioClass: 'iradio_square-blue'
                });
            } else {
                layer.msg(data.errorMsg)
            }

        },
        error: function () {
        }
    });
}

function queryParams(params) {
    return {
        'pageUnit.size': params.pageSize,
        'pageUnit.page': params.pageNumber,
        userAccount: $("#userAccount").val(),
        productType: $("#productType").val(),
        supplyModel: $("#supplyModel").val(),
    };
}